/* twi_hw.h

Copyright (C) Alex S. (mailto: md5sum@alexsp.ru)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details
http://www.gnu.org/licenses/gpl-2.0.html.

You should have received a copy of the GNU General Public License
along with this program in the file gpl-2.0.txt;
if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


#ifndef TWI_HW_H_
#define TWI_HW_H_

#include "global.h"
#include <util/twi.h>

#define TWI_wait()  while(!(TWCR&(1<<TWINT)));TWI_last_error=TW_STATUS
#define TWI_start()  TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN)|(1<<TWSTO);TWI_wait()
#define TWI_restart()  TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);TWI_wait()
#define TWI_stop()  TWCR=(1<<TWINT)|(1<<TWSTO)|(1<<TWEN)
#define TWI_send_data_nack(a)   TWDR=a;TWCR=(1<<TWINT)|(1<<TWEN);TWI_wait()
#define TWI_send_data_ack(a)   TWDR=a;TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);TWI_wait()
#define TWI_check(a) (TW_STATUS==a?0:1)
extern uint8_t TWI_last_error;

void TWI_init();

#endif /* TWI_HW_H_ */
